/**
 * Created by Dalyn on 2017/3/15.
 * 贷款设置-弹框
 */
import React, {Component}from 'react';
import Relay from "react-relay";
import ReactKey from "../../../../../util/ReactKey"
import Util from "../../../../../util/Util"
import "./css/materielBom.css"
import InputBox from "../../../../../common/InputBox";
import {Select,Input,Icon,message} from "antd";
const Option = Select.Option;
import AddOrEditMaterielBillMutation from "./mutation/AddOrEditMaterielBillMutation"
import {_Icon,_Modal} from 'ANT_DE';
const Modal = _Modal;

export default class AddMaterielModal extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            //materielBillData:[],
            styleNoList:[],
            materielTypeList:[
                {value:"1",text:"面料"},
                {value:"2",text:"缝制辅料"},
                {value:"3",text:"包装辅料"},
                ],
            styleNo:"",
            materielType:"",
            saveMaterielBill:{
                "bomId":"",
                "orderId":"",
                "goodsId":"",
                "styleNo":"",
                "materielType":"",
                "size":"",
                "productName":"",
                "color":"",
                "description":"",
                "quantity":"",
                "dosage":"",
                "consume":"",
                "needCount":"",
                "prickle":"",
                "partDesc":"",
            },
            hint_obj:{
                "styleNo":"",
                "materielType":"",
                "size":"",
                "productName":"",
                "color":"",
                "description":"",
                "quantity":"",
                "dosage":"",
                "consume":"",
                "needCount":"",
                "prickle":"",
                "partDesc":"",
            },
            confirmType:"",
        };
        this.style_ = {
            addInput: {

            },
        };
        this.isshow = 'none';
        this.isRelay = true;
        this.messageFlag = true;
        //this.materielBillData=[];
        this.orderGoodsList=[];
        this.materielBill = ["styleNo","materielType","size", "productName", "color","description","quantity", "dosage", "consume", "needCount", "prickle", "partDesc",];
        this.materielBillName = {//12个
            "styleNo":"款号",
            "materielType":"物料类型",
            "size":"尺码",
            "productName":"品名",
            "color":"颜色",
            "description":"描述/规格",
            "quantity":"数量",
            "dosage":"用量/件",
            "consume":"损耗",
            "needCount":"需求数",
            "prickle":"单位",
            "partDesc":"用处与位置说明",
        };
        this.saveMaterielBill = {
            "bomId":"",
            "orderId":"",
            "goodsId":"",
            "styleNo":"",
            "materielType":"",
            "size":"",
            "productName":"",
            "color":"",
            "description":"",
            "quantity":"",
            "dosage":"",
            "consume":"",
            "needCount":"",
            "prickle":"",
            "partDesc":"",
        };
        this.materielTypeObj={
            "1":"面料",
            "2":"缝制辅料",
            "3":"包装辅料",
        };
        this.addMatialFlag=true;
        //清空所有提示的时候用
        this.hint_obj={
            "styleNo":"",
                "materielType":"",
                "size":"",
                "productName":"",
                "color":"",
                "description":"",
                "quantity":"",
                "dosage":"",
                "consume":"",
                "needCount":"",
                "prickle":"",
                "partDesc":"",
        };
    }

    componentWillMount() {
        //this.materielBillData=this.props.materielBillData;
        this.orderGoodsList=this.props.orderGoodsList;
        this.setState({
            materielBillData:this.props.materielBillData,
            orderGoodsList:this.props.orderGoodsList,
        })
    }
    componentWillReceiveProps(nextProps){
        this.orderGoodsList=nextProps.orderGoodsList;
        this.setState({
            materielBillData:nextProps.materielBillData,
            orderGoodsList:nextProps.orderGoodsList,
        })
    }
    /*input输入回调*/
    addInputChange(type,e){
        let hint_obj = JSON.parse(JSON.stringify(this.state.hint_obj));
        let this_ = this;
        let text = e.target.value;
        let {saveMaterielBill} = this.state;
        let emptyReg = /^\s+$/;
        let numReg = /^([0-9]{0,5})+\.?[0-9]{0,4}$/;
        switch (type){
            case "dosage":
                if(text&&!numReg.test(text)||Number(text)>99999.9999){
                    hint_obj[type] = "格式错误";
                    return;//跳出整个方法
                }else {
                    hint_obj[type] = ""
                }
                break;
            case "consume":
                if(text&&!numReg.test(text)||Number(text)>99999.9999){
                    hint_obj[type] = "格式错误";
                    return;
                }else {
                    hint_obj[type] = ""
                }
                break;
            default:
                hint_obj[type] = "";
                this.setState({
                    hint_obj:hint_obj,
                });
                break;
        }

        //input赋值
        saveMaterielBill[type] = text;
        this.setState({
            saveMaterielBill:saveMaterielBill,
            hint_obj:hint_obj,
        })
    }
    /*保存请求*/
    relayAdd(flag) {
        let {confirmType,saveMaterielBill} = this.state;
        Relay.Store.commitUpdate(
            new AddOrEditMaterielBillMutation(saveMaterielBill),
            {
                onFailure: () => {
                    console.log("请求失败")
                },
                onSuccess: (response) => {
                    let errorCode = response.addOrEditMaterielBill.addOrEditMaterielBill.errorCode;
                    let value = response.addOrEditMaterielBill.addOrEditMaterielBill.value;
                    if (errorCode === "0") {
                        if(confirmType == "add"){
                            message.success("添加成功");
                        }else if(confirmType == "edit"){
                            message.success("编辑成功");
                        }else {
                            message.success("保存成功");
                        }
                        this.props.findMaterielBill(1000,1);
                        this.setState({
                            visible:  !flag,
                            saveMaterielBill:JSON.parse(JSON.stringify(this.saveMaterielBill)),
                            hint_obj:JSON.parse(JSON.stringify(this.hint_obj)),//清空数据
                        });
                    } else {
                        let this_ = this;
                        if(errorCode == 500){
                            Util.messageHint(this_,"error","服务器错误，请联系系统管理员");
                        }else if(errorCode == 9999){
                            Util.messageHint(this_,"error","系统错误，请联系系统管理员");
                        }else {
                            Util.messageHint(this_,"error",value);
                        }

                    }
                }
            }
        );
    }

    /*点击确认*/
    handleConfirm(flag) {
        // debugger
        let {styleNo,materielType,saveMaterielBill,hint_obj} = this.state;
        hint_obj = JSON.parse(JSON.stringify(hint_obj));
        this.addMatialFlag=true;
        let this_ = this;
        let regName =/^\s*$/g;
        let message = "";
        let cur = "";
        for(let i =0; i<this.materielBill.length;i++){
            // debugger
            let v = this.materielBill[i];
            if(v == "styleNo"){
                if(!saveMaterielBill.styleNo){
                    this.addMatialFlag = false;
                    cur = v;
                    // message = this.materielBillName[v]+"不能为空";
                    // Util.messageHint(this_,"warning",message);
                    for(let attr in this.materielBillName){
                        if(attr == v){
                            hint_obj[v] = this.materielBillName[v]+"不能为空";
                        }else {
                            hint_obj[attr] = "";
                        }
                    }
                    this.setState({
                        hint_obj:hint_obj,
                    });
                    break
                }else {
                    for(let attr in this.materielBillName){
                        hint_obj[attr] = "";
                    }
                    continue
                }
            }else if(v == "materielType"){
                if(!saveMaterielBill.materielType){
                    this.addMatialFlag = false;
                    cur = v;
                    // message = this.materielBillName[v]+"不能为空";
                    // Util.messageHint(this_,"warning",message);
                    for(let attr in this.materielBillName){
                        if(attr == v){
                            hint_obj[v] = this.materielBillName[v]+"不能为空";
                        }else {
                            hint_obj[attr] = "";
                        }
                    }
                    this.setState({
                        hint_obj:hint_obj,
                    });
                    break
                }else {
                    for(let attr in this.materielBillName){
                        hint_obj[attr] = "";
                    }
                    continue
                }
            }else if(v == "partDesc"){

            }else if(v == "needCount"){
                let value = this.refs["add"+v].value;
                if(regName.test(value)){
                    this.addMatialFlag = false;
                    cur = v;
                    // message = this.materielBillName[v]+"不能为空";
                    // Util.messageHint(this_,"warning",message);
                    for(let attr in this.materielBillName){
                        if(attr == v){
                            hint_obj[v] = this.materielBillName[v]+"不能为空";
                        }else {
                            hint_obj[attr] = "";
                        }
                    }
                    this.setState({
                        hint_obj:hint_obj,
                    });
                    break
                }else if(value == 0){
                    this.addMatialFlag = false;
                    cur = v;
                    // message = this.materielBillName[v]+"不能为空";
                    // Util.messageHint(this_,"warning",message);
                    for(let attr in this.materielBillName){
                        if(attr == v){
                            hint_obj[v] = this.materielBillName[v]+"不能为零";
                        }else {
                            hint_obj[attr] = "";
                        }
                    }
                    this.setState({
                        hint_obj:hint_obj,
                    });
                    break
                }else {
                    for(let attr in this.materielBillName){
                        hint_obj[attr] = "";
                    }
                    continue
                }
            }else {
                let value = this.refs["add"+v].value;
                if(regName.test(value)){
                    this.addMatialFlag = false;
                    cur = v;
                    // message = this.materielBillName[v]+"不能为空";
                    // Util.messageHint(this_,"warning",message);
                    for(let attr in this.materielBillName){
                        if(attr == v){
                            hint_obj[v] = this.materielBillName[v]+"不能为空";
                        }else {
                            hint_obj[attr] = "";
                        }
                    }
                    this.setState({
                        hint_obj:hint_obj,
                    });
                    break
                }else {
                    for(let attr in this.materielBillName){
                        hint_obj[attr] = "";
                    }
                    continue
                }
            }
        }
        this.materielBill.forEach((v,i)=>{
            //forEach 没有跳出整个循环的方法
        });
        if(this.addMatialFlag){
            this.relayAdd(flag);
        }
    }
    /*父组件调用*/
    showWindow(flag){
        this.setState({
            visible: !flag,
        });
    }
    /*点击取消*/
    handleCancel(flag) {
        this.setState({
            visible: !flag,
            saveMaterielBill:JSON.parse(JSON.stringify(this.saveMaterielBill)),//清空数据
            hint_obj:JSON.parse(JSON.stringify(this.hint_obj)),//清空数据
        });

    }
    /*下拉框选中回调*/
    selectChange(type,value){
        let {styleNo,materielType,saveMaterielBill,hint_obj} = this.state;
        switch (type){
            case "styleNo":
                this.orderGoodsList.map((v,i)=>{
                    if(v.gid == value){
                        saveMaterielBill.goodsId = v.gid;
                        saveMaterielBill.styleNo = v.styleNo;
                        saveMaterielBill.orderId = v.orderId;
                        saveMaterielBill.quantity = v.quantity;
                        hint_obj[type]="";
                        this.setState({
                            saveMaterielBill:saveMaterielBill,
                            hint_obj:hint_obj,
                        });
                        return
                    }
                });
                break;
            case "materielType":
                saveMaterielBill.materielType = value;
                hint_obj[type]="";
                this.setState({
                    saveMaterielBill:saveMaterielBill,
                    hint_obj:hint_obj,
                });
                break;
            default:
                break;
        }
    }
    returnSelectList(type){
        let {materielTypeList,styleNoList} = this.state;
        switch (type){
            case "styleNo":
                styleNoList&&styleNoList.map((v,i)=>{
                    return(
                        <Option key={v.uid} value={v.uid}>{v.text}</Option>
                    )
                });
                break;
            case "materielType":
                materielTypeList&&materielTypeList.map((v,i)=>{
                    // console.log(type);
                    // console.log(v.text);
                    return(
                        <Option key={v.uid} value={v.uid}>{v.text}</Option>
                    )
                });
                break;

        }

    }
    /* 关于数量的4个input -- 数量 用量/件 损耗 需求数*/
    inputNumLimit(item){
        let {saveMaterielBill} = this.state;
        if(item == "quality"){
            return(
                1
            )
        }else if(item == "dosage"){
            return(
                1
            );
        }else if(item == "consume"){
            return(
               1
            );
        }else if(item == "needCount"){
            return(
                1
            );
        }
    }
    /*8个input*/
    addInput(){
        let { materielTypeList,saveMaterielBill,hint_obj} = this.state;
        let selectClass = (item)=>hint_obj[item]?"order_creat_select materielBom_set_select select_border_red":"order_creat_select materielBom_set_select";
        let inputClass = (item)=>hint_obj[item]?"materielBom_set_inputSty input_border_red":"materielBom_set_inputSty";
        let needCount = ()=>{
            let num  = Math.round(Number(saveMaterielBill.quantity)*Number(saveMaterielBill.dosage)*Number(saveMaterielBill.consume));
            return num?num:0;
        };
        // console.log(hint_obj);
        return this.materielBill.map((item,index)=>{
            if(item == "styleNo"){//款号
                return (
                    <div className="materielBom_set_input materielBom_set_float" ref={"selectBox_styleNo"}>
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <Select
                            ref={"add"+item}
                            key={item}
                            style={{ width: 155 }}
                            className={selectClass(item)}
                            dropdownClassName="order_creat_select_drop "
                            placeholder={"请选择"+this.materielBillName[item]}
                            value={saveMaterielBill[item]?saveMaterielBill[item]:"请选择"+this.materielBillName[item]}
                            notFoundContent="无"
                            optionFilterProp="children"
                            getPopupContainer={()=>this.refs.selectBox_styleNo}
                            defaultActiveFirstOption={false}
                            onChange={this.selectChange.bind(this,item)}
                            filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                        >
                            {
                                this.orderGoodsList&&this.orderGoodsList.map((v,i)=>{
                                    return(
                                        <Option key={v.gid} value={v.gid}>{v.styleNo}</Option>
                                    )
                                })
                            }
                        </Select>
                        <p className="materielBom_set_spanHint">{hint_obj[item]}</p>
                    </div>
                )
            }else if(item == "materielType"){//物料类型
                return (
                    <div className="materielBom_set_input materielBom_set_float" ref={"selectBox_materielType"}>
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <Select
                            ref={"add"+item}
                            key={item}
                            style={{ width: 155 }}
                            className={selectClass(item)}
                            dropdownClassName="order_creat_select_drop"
                            placeholder={"请选择"+this.materielBillName[item]}
                            value={saveMaterielBill[item]?this.materielTypeObj[saveMaterielBill[item]]:"请选择"+this.materielBillName[item]}
                            notFoundContent="无"
                            optionFilterProp="children"
                            getPopupContainer={()=>this.refs.selectBox_materielType}
                            defaultActiveFirstOption={false}
                            onChange={this.selectChange.bind(this,item)}
                            filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                        >
                            {
                                materielTypeList&&materielTypeList.map((v,i)=>{
                                    return(
                                        <Option key={v.value} value={v.value}>{v.text}</Option>
                                    )
                                })
                            }
                        </Select>
                        <p className="materielBom_set_spanHint">{hint_obj[item]}</p>
                    </div>
                )
            }else if(item == "partDesc"){//用处与位置说明 12
                return (
                    <div className="materielBom_set_input materielBom_set_float">
                        <span className="materielBom_set_inputspan">{this.materielBillName[item]}：</span>
                        <input
                            type="text"
                            className={inputClass(item)}
                            placeholder ={"请输入"+this.materielBillName[item]}
                            value={saveMaterielBill[item]}
                            ref={"add"+item}
                            onChange={this.addInputChange.bind(this,item)}
                            maxLength="100"
                        />
                        {/*<p className="materielBom_set_spanHint">{hint_obj[item]}</p>*/}
                    </div>
                )
            }else if(item == "quantity"){//数量 7
                return (
                    <div className="materielBom_set_input materielBom_set_float">
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <input
                            type="text"
                            className={inputClass(item)}
                            //placeholder ={"请输入"+this.materielBillName[item]}
                            value={saveMaterielBill[item]}
                            ref={"add"+item}
                            maxLength="50"
                            disabled="true"
                        />
                        {/*<p className="materielBom_set_spanHint">{hint_obj[item]}</p>*/}
                    </div>
                )
            }else if(item == "dosage"){//用量/件 8
                return (
                    <div className="materielBom_set_input materielBom_set_float">
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <input
                            type="text"
                            className={inputClass(item)}
                            placeholder ={"请输入"+this.materielBillName[item]}
                            value={saveMaterielBill[item]}
                            ref={"add"+item}
                            onChange={this.addInputChange.bind(this,item)}
                            maxLength="50"
                        />
                        <p className="materielBom_set_spanHint">{hint_obj[item]}</p>
                    </div>
                )
            }else if(item == "consume"){//损耗 9
                return (
                    <div className="materielBom_set_input materielBom_set_float">
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <input
                            type="text"
                            className={inputClass(item)}
                            placeholder ={"请输入"+this.materielBillName[item]}
                            value={saveMaterielBill[item]}
                            ref={"add"+item}
                            onChange={this.addInputChange.bind(this,item)}
                            maxLength="50"
                        />
                        <p className="materielBom_set_spanHint">{hint_obj[item]}</p>
                    </div>
                )
            }else if(item == "needCount"){//需求数 10
                return (
                    <div className="materielBom_set_input materielBom_set_float">
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <input
                            type="text"
                            className={inputClass(item)}
                            value ={needCount()}
                            ref={"add"+item}
                            maxLength="50"
                            disabled="true"
                        />
                        <p className="materielBom_set_spanHint">{hint_obj[item]}</p>
                    </div>
                )
            }else if(item == "size"||item == "productName"||item == "color"||item == "prickle"){// 尺码 品名 颜色 单位 2 3 4 11
                return(
                    <div className="materielBom_set_input materielBom_set_float">
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <input
                            type="text"
                            className={inputClass(item)}
                            placeholder ={"请输入"+this.materielBillName[item]}
                            value={saveMaterielBill[item]}
                            ref={"add"+item}
                            onChange={this.addInputChange.bind(this,item)}
                            maxLength="50"
                        />
                        <p className="materielBom_set_spanHint">{hint_obj[item]}</p>
                    </div>
                );
            }else if(item == "description"){// 描述/规格 6
                return(
                    <div className="materielBom_set_input materielBom_set_float">
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <input
                            type="text"
                            className={inputClass(item)}
                            placeholder ={"请输入"+this.materielBillName[item]}
                            value={saveMaterielBill[item]}
                            ref={"add"+item}
                            onChange={this.addInputChange.bind(this,item)}
                            maxLength="100"
                        />
                        <p className="materielBom_set_spanHint">{hint_obj[item]}</p>
                    </div>
                );
            }else {
                return(
                    <div className="materielBom_set_input materielBom_set_float">
                        <span className="materielBom_set_inputspan"><i className="red">*</i>{this.materielBillName[item]}：</span>
                        <input
                            type="text"
                            className={inputClass(item)}
                            placeholder ={"请输入"+this.materielBillName[item]}
                            value={saveMaterielBill[item]}
                            ref={"add"+item}
                            onChange={this.addInputChange.bind(this,item)}
                            maxLength="100"
                        />
                        <p className="materielBom_set_spanHint">{hint_obj[item]}</p>
                    </div>
                );
            }
        })
    }
    render() {
        //console.log(this.state.saveMaterielBill);
        let {confirmType } = this.state;
        return (
            <Modal
                className="batchhint_modal"
                visible={this.state.visible}
                onCancel={this.handleCancel.bind(this, this.state.visible)}
                onOk={this.handleConfirm.bind(this, this.state.visible)}
                title={confirmType=="add"?"添加物料":"编辑物料"}
                maskClosable={false}
            >
                <div className="materielBom_set_up clear">
                    {
                        this.addInput()
                    }
                </div>
            </Modal>
        )
    }
}